-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
39 lines (32 loc) · 906 Bytes
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
if (n < 2) {
cout << "Error: Array must have at least two elements." << endl;
return 1;
}
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int largest = INT_MIN, secondLargest = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
if (secondLargest == INT_MIN) {
cout << "There is no second largest element." << endl;
} else {
cout << "The second largest number is: " << secondLargest << endl;
}
return 0;
}